package nemeanshop;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Ragnar
 */
public class NemeanShop 
{
    private Database            db;
    private ArrayList<Product>  products;
    private ArrayList<Cashier>  cashiers;
    private Cashier             cash;
    private Order               order;
    private Crypt               c = new Crypt();

    public NemeanShop()
    {
        this.db = new Database();
        this.products =  getAllProducts();
        this.cashiers =  getAllCashiers();
    }
    
    
    public void createOrder(){
        
        this.order = new Order(cash);
    }
    
    public Order getOrder(){
        return this.order;
    }
    
    public void deleteOrder()
    {
        this.order = null;
    }

    public boolean addProduct(Product p)
    {
        if (this.db.addProduct(p.getBarcode(), p.getProductName(), p.getStockPrice(), p.getSellPrice(), p.getQty()))
        {
            this.products = getAllProducts();
            return true;
        }
        
        else return false;
    }
    
    public boolean deleteProduct(String bcode)
    {
        if (this.isProduct(bcode) && this.productExists(bcode))
        {
            return this.db.deleteProduct(bcode);
        }
        return false;
    }
    
    public boolean updateProductQuantity(String bcode, int quantity)
    {
        if (this.isProduct(bcode) && this.productExists(bcode))
        {
            try
            {
                Product p = this.getProduct(bcode);
                if ((p.getQty() + quantity) >= 0)
                {
                    return this.db.updateProductQuantity(bcode, quantity);
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Vi kan ikke ha et negativt antall varer!");
                }
            }
            catch (NemeanShopException ex)
            {
                Logger.getLogger(NemeanShop.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return false;
    }


    public synchronized ArrayList<Product> getAllProducts()
    {
        return this.db.getAllProducts();
    }
    
    
    
    public boolean productExists(String bcode){
        
        if(this.db.getProduct(bcode)!= null){
            return true;
        }
        
        else return false;
        
    }
    
    public boolean isProduct(String barcode)
    {
        for (Product p : this.products)
        {
            if (p.getBarcode().equals(barcode))
            {
                return true;
            }    
        }
        return false;
    }
    
    
    public ArrayList<Cashier> getAllCashiers(){
        
        return this.db.getAllCashiers();
    }
    
    public boolean AuthorizeCashier(String bc){
        
        for (Cashier c : this.cashiers)
        {
            if (c.getBarcode().equals(bc))
            {
                this.cash = c;
                return true;
            }
        }
        return false;
    }
    
    public boolean isAuthorized()
    {
        return (cash == null) ? false : true;
    }
    
    public void removeCashier(){
        
        this.cash = null;
    }
    
    public Cashier getCashier()
    {
        return (cash instanceof Cashier) ? this.cash : null;
    }
    
    public Product getProduct(String barcode) throws NemeanShopException
    {
        for (Product p : this.products)
        {
            if (p.getBarcode().equals(barcode))
            {
                return p;
            }
        }
        throw new NemeanShopException("Product was not found!");
    }
    
    public ArrayList<Product> getProducts()
    {
        return this.products;
    }
    
    /**
     * For å hente ut en selger i fra listen.
     * 
     * 
     * @param barcode
     * @return
     * @throws NemeanShopException 
     */
    public Cashier getCashier(String barcode)
    {
        for (Cashier c : this.cashiers)
        {
            if (c.getBarcode().equals(barcode))
            {
                return c;
            }
        }
        try {
            throw new NemeanShopException("Cashier was not found!");
        } catch (NemeanShopException ex) {
            ex.getMsg();
            return null;
        }
    }
    
    public ArrayList<Cashier> getCashiers()
    {
        return this.cashiers;
    }
    
    public boolean logIn(String un, String pw) {
        String s = this.db.logIn(un, pw);
        if(AuthorizeCashier(s)) return true;
        
        else return false;
        
    }
    
    public String loginBc(String un, String pw){
        
            return this.db.logIn(un, pw);
        
    }
    
    
    public boolean registerOrder(Order o){
        
        if(db.registerOrder(o)){
            this.products = this.getAllProducts();
            return true;
        }
        return false;
        
    }
    
    public boolean registerAdminOrder(Order o, int id){
        
        if(db.registerAdminOrder(o, id)){
            this.products = this.getAllProducts();
            return true;
        }
        return false;
        
    }
    
    public double getProfit(){
        
        if(db.getProfit()>0){
            return db.getProfit();
            
        }
        else return 0;
    }
    
    public boolean checkProducts(){
        this.products = this.getAllProducts();
        int count = this.products.size();
        
        for(int i = 0; i<count; i++){
        
            if(this.products.get(i).getQty()<10){
                
                return true;
            }
        }
        return false;
        
    }
}
